package com.lxq.particledemo02;


import android.opengl.Matrix;

import java.util.Random;

public class ParticleShooter {

    private final float angleVariance;
    private final float speedVariance;

    private final Random random = new Random();

    private float[] rotationMatrix = new float[16];
    private float[] directionMatrix = new float[4];
    private float[] resultVactor = new float[4];


    private final Point3 position;
    private final Vector3 direction;
    private final int color;

    public ParticleShooter(Point3 position, Vector3 direction, int color, float angle, float speed) {
        this.position = position;
        this.direction = direction;
        this.color = color;
        this.angleVariance = angle;
        this.speedVariance = speed;

        directionMatrix[0] = direction.Vx;
        directionMatrix[1] = direction.Vy;
        directionMatrix[2] = direction.Vz;

    }

    public void addParticles(ParticleSystem particleSystem, float currentTime,
                             int count) {
        for (int i = 0; i < count; i++) {

            Matrix.setRotateEulerM(rotationMatrix, 0, (random.nextFloat() - 0.5f) * angleVariance, (random.nextFloat() - 0.5f) * angleVariance, (random.nextFloat() - 0.5f) * angleVariance);
            Matrix.multiplyMV(resultVactor, 0, rotationMatrix, 0, directionMatrix, 0);

            float speedAdjustment = 1f + random.nextFloat() * speedVariance;

            Vector3 temDirection = new Vector3(resultVactor[0] * speedAdjustment, resultVactor[1] * speedAdjustment, resultVactor[2] * speedAdjustment);

            particleSystem.addParticle(position, color, temDirection, currentTime);
        }
    }
}
